/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef EMULEX_INTERFACE_CARD_H
#define EMULEX_INTERFACE_CARD_H

#include <mc/engine.h>
#include <mc/core/timer.h>
#include <pldm_over_mctp/pldm_over_mctp_emulex.h>
#include <imu/imu.h>

#include "interface/board.h"
#include "interface/network_adapter.h"
#include "interface/network_adapter/cooling.h"
#include "interface/pcie_card.h"
#include "interface/pcie_device.h"
#include "emulex_port.h"
#include "interface/pcie_device/bandwidth.h"

namespace dev {
using pldm_over_mctp_emulex_ptr = std::shared_ptr<pldm_over_mctp_emulex>;
using imu_ptr               = std::shared_ptr<imu>;

class emulex_card : public mc::engine::object<emulex_card> {
public:
    MC_OBJECT(
        emulex_card, "PCIeNicCard", "/bmc/dev/Systems/1/PCIeNicCard/${object_name}",
        (PCIeDevice)(PCIeCard)(NetworkAdapter_Cooling)(Board)(NetworkAdapter)(PCIeDevice_Bandwidth))
    emulex_card();
    ~emulex_card();
    bool start();
    bool stop();
    bool init(mc::mutable_dict& csr_object, const mc::dict& connector);
    void init_network_ports();
    void update_ports_bdf_async(imu_pcie_device_req_t& pcie_device_info);
    void replace_dynamic_property(mc::mutable_dict& object, const mc::dict& connector);

    

    PCIeDevice             m_pcie_device;
    PCIeCard               m_pcie_card;
    NetworkAdapter         m_network_adapter;
    NetworkAdapter_Cooling m_network_adapter_cooling;
    Board                  m_board;
    uint8_t                m_system_id;
    PCIeDevice_Bandwidth   m_pcie_device_bandwidth;

private:
    std::vector<emulex_port*> m_network_ports;

private:
    bool                  start_pldm_protocol();
    void                  stop_pldm_protocol();
    void                  handle_os_reset();
    bool                  start_protocol();
    bool                  init_imu_protocol();
    pldm_over_mctp_emulex_ptr m_pldm_over_mctp_emulex;
    imu_ptr               m_imu_obj;
    mc::milliseconds      m_interval = mc::milliseconds(5000);  // pldm over mctp 更新间隔5s
    mctp*                 m_mctp_object;
    mc::core::timer*      m_pldm_timer;
};

} // namespace dev

#endif // __emulex_CARD_H__